package com.zy.batiya.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

  /**
   * 获取秒级时间戳
   * @return
     */
  public static int secondTimestamp(){
    Long l = new Date().getTime()/1000;
    return l.intValue();
  }

  /** 返回今日字符串格式日期
   * 
   * @return
   */
  public static String getDateStr(String pattern,Date date){
    SimpleDateFormat sf = new SimpleDateFormat(pattern);
    String rs = sf.format(date);
    return rs;
  }
  
  public static Date getDateByStr(String pattern,String dateStr) throws ParseException{
    SimpleDateFormat sf = new SimpleDateFormat(pattern);
    return sf.parse(dateStr);
  }
  
  public static Date getYesterDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    date = calendar.getTime();
    return date;
  }
  
  public static Date getOffsetDay(Date date,int offset) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, offset);
    date = calendar.getTime();
    return date;
  }
  
  public static String getOffsetDay(Date date,int offset,String pattern) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, offset);
    date = calendar.getTime();
    SimpleDateFormat sf = new SimpleDateFormat(pattern);  
    return sf.format(date);
  }
  
  public static String getOffsetMonth(String dateStr,int offset,String pattern) throws ParseException {
    SimpleDateFormat sf = new SimpleDateFormat(pattern);  
    Date date = sf.parse(dateStr);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, -1);
    date = calendar.getTime();
    return sf.format(date);
  }
  
  
  public static String getOffsetDay(String dateStr,int offset,String pattern) throws ParseException {
    SimpleDateFormat sf = new SimpleDateFormat(pattern);  
    Date date = sf.parse(dateStr);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, offset);
    date = calendar.getTime();
    return sf.format(date);
  }
  
  /** 返回差了几天
   * 
   * @param begin
   * @param end
   * @param pattern
   * @return
   * @throws ParseException 
   */
  public static int getOffset(String begin,String end,String pattern) throws ParseException{
    int rs = 0;
    String addDate = "";
    SimpleDateFormat sf = new SimpleDateFormat(pattern);  
    Date date = sf.parse(begin);
    
    while(!addDate.equals(end)){
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, 1);
      date = calendar.getTime();
      addDate = DateUtil.getDateStr(pattern, date);
      rs++;
    }
    return rs+1;
  }
  
  /** 返回时间区间dt
   * 
   * @param begin
   * @param end
   * @param pattern
   * @return
   * @throws ParseException 
   */
  public static List<String> getDtList(String begin,String end,String pattern) throws ParseException{
    List<String> rs = new ArrayList<String>();
    String addDate = begin;
    SimpleDateFormat sf = new SimpleDateFormat(pattern);  
    Date date = sf.parse(begin);
    rs.add(begin);
    while(!addDate.equals(end)){
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, 1);
      date = calendar.getTime();
      addDate = DateUtil.getDateStr(pattern, date);
      rs.add(addDate);
    }
    return rs;
  }
  
  public static void main(String[] args) {
    System.out.println(getYesterDay(new Date()));
  }
}
